/**
 * 
 */
package cz.cvut.fit.bugtrac.dao;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import cz.cvut.fit.bugtrac.exception.BugTracException;
import cz.cvut.fit.bugtrac.persistence.Graph;
import cz.cvut.fit.bugtrac.publicmodel.dto.graph.GraphDTO;
import cz.cvut.fit.bugtrac.util.DozerUtil;

/**
 * @author Jarec
 * 
 */
@Component
@Transactional
public class GraphDAOImpl extends AbstractBugTracDAO<Graph> implements GraphDAO {

	private static final String SELECT_FOR_SNAPSHOTS = "SELECT g FROM Graph g ORDER BY g.created ASC";
	private static final String SELECT_FOR_SNAPSHOTS_DESC = "SELECT g FROM Graph g ORDER BY g.created DESC";
	private DozerUtil dozerUtil;

	@Override
	public Class<Graph> getEntityClass() {
		return Graph.class;
	}

	@Override
	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true)
	public Collection<Graph> getSnapshots() throws BugTracException {
		return em.createQuery(SELECT_FOR_SNAPSHOTS).getResultList();
	}

	@Override
	@Transactional(readOnly = true)
	public Collection<GraphDTO> getSnapshotsDTO() throws BugTracException {
		return dozerUtil.mapList(em.createQuery(SELECT_FOR_SNAPSHOTS)
				.getResultList(), GraphDTO.class);
	}

	@Override
	@Transactional(readOnly = true)
	public GraphDTO getLastSnapshots() throws BugTracException {
		return dozerUtil.mapObject(em.createQuery(SELECT_FOR_SNAPSHOTS_DESC)
				.getResultList().get(0), GraphDTO.class);
	}

	/**
	 * @return the dozerUtil
	 */
	public DozerUtil getDozerUtil() {
		return dozerUtil;
	}

	/**
	 * @param dozerUtil
	 *            the dozerUtil to set
	 */
	@Autowired
	public void setDozerUtil(DozerUtil dozerUtil) {
		this.dozerUtil = dozerUtil;
	}

}
